import { createSlice } from "@reduxjs/toolkit";

interface ArticleTempDataState {
  shortToken: string;
  longToken: string;
}

const tokenTempDataSlice = createSlice({
  name: "articleTempData",
  initialState: {
    shortToken: localStorage.getItem("shortToken") || "",
    longToken: localStorage.getItem("longToken") || "",
  } as ArticleTempDataState,
  reducers: {
    // 设置短令牌
    setShortToken(state, action) {
      state.shortToken = action.payload;
      localStorage.setItem("shortToken", action.payload);
    },

    // 清除短令牌
    clearShortToken(state) {
      state.shortToken = "";
      localStorage.removeItem("shortToken");
    },

    // 设置长令牌
    setLongToken(state, action) {
      state.longToken = action.payload;
      localStorage.setItem("longToken", action.payload);
    },

    // 清除长令牌
    clearLongToken(state) {
      state.longToken = "";
      localStorage.removeItem("longToken");
    },
  },
});

// 获取文章列表数据action
const { setShortToken, clearShortToken, setLongToken, clearLongToken } =
  tokenTempDataSlice.actions;

// 获取文章列表数据reducer
const articleTempDataReducer = tokenTempDataSlice.reducer;

// 导出action
export { setShortToken, clearShortToken, setLongToken, clearLongToken };

// 默认导出文章列表数据reducer
export default articleTempDataReducer;
